from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import redirect
from django.urls import reverse
from myadmin.models import Orders, OrdersDetail, Payment, Member, Shop
from utils.encrypt import md5


def index(request):
    """ 个人中心首页 """
    return render(request, 'mobile/member.html')


def orders(request):
    """ 个人中心浏览订单 """
    """ 浏览订单 """
    # 获取当前店铺id号
    mid = request.session['mobileuser']['id']  # 获取当前会员的id号
    olist = Orders.objects.filter(member_id=mid)

    # 搜索条件
    mywhere = []
    status = request.GET.get('status', '')
    if status != '':
        olist = olist.filter(status=status)
        # mywhere.append('status=' + status)

    list2 = olist.order_by('-id')  # 排序

    # 订单状态
    orders_status = ['无', '排队中', '已撤销', '已完成']
    # 遍历订单，在封装
    for vo in list2:
        plist = OrdersDetail.objects.filter(order_id=vo.id)[:4]
        vo.plist = plist
        vo.statusinfo = orders_status[vo.status]

    sta = request.GET.get('status', 2)
    # print(type(sta))
    context = {
        'orderslist': list2,
        'sta': sta
    }
    return render(request, 'mobile/member_orders.html', context)


def detail(request):
    """ 个人中心订单详情 """
    pid = request.GET.get('pid', 0)
    # 获取当前订单
    order = Orders.objects.get(id=pid)
    # 获取订单详情
    plist = OrdersDetail.objects.filter(order_id=order.id)
    order.plist = plist
    # 获取店铺名
    shop = Shop.objects.only('name').get(id=order.shop_id)
    order.shop_name = shop.name
    # 订单状态
    orders_status = ['无', '排队中', '已撤销', '已完成']
    order.statusinfo = orders_status[order.status]

    return render(request, 'mobile/member_detail.html', {'order': order})


def logout(request):
    """ 会员退出 """
    # 退出删除session中的会员信息和店铺信息
    del request.session['mobileuser']
    # del request.session['shopinfo']
    return redirect(reverse('mobile_register'))


def set_password(request):
    """ 修改密码页面 """
    print(request.session['mobileuser'])
    context = {
        'password': request.session['mobileuser']['password']
    }
    return render(request, 'mobile/set_password.html', context)


def do_set_password(request):
    """ 执行修改密码 """
    old_pass = request.POST.get('old_pass')
    new_pass = request.POST.get('new_pass')
    confirm_pass = request.POST.get('confirm_pass')
    # print(old_pass, new_pass, confirm_pass)

    mobile_id = request.session['mobileuser']['id']
    mobile_user = Member.objects.get(id=mobile_id)

    if mobile_user.password:
        if md5(mobile_user.password) != md5(old_pass):
            context = {'info': '旧密码输入错误'}
            return render(request, 'mobile/set_password.html', context)
            # return redirect(reverse('mobile_set_password'), context)

    if md5(new_pass) != md5(confirm_pass):
        context = {'info': '新密码和确认密码不一样'}
        return render(request, 'mobile/set_password.html', context)

    # 设置密码
    mobile_user.password = md5(new_pass)
    # print(mobile_user.password)
    mobile_user.save()

    # 修改session中的值
    request.session['mobileuser']['password'] = md5(new_pass)
    print(request.session['mobileuser'])
    # request.session['mobileuser'] = request.session['mobileuser']
    # 重定向
    return redirect(reverse('mobile_index'))


def information(request):
    orders_num = Orders.objects.filter(member_id=request.session['mobileuser']['id'], status=3)
    # print(len(orders_num))
    context = {
        'orders_num': len(orders_num),
        'orders_num_1': len(Orders.objects.filter(member_id=request.session['mobileuser']['id'], status=1))
    }
    return render(request, 'mobile/information.html', context)


def do_information(request):
    return render(request, 'mobile/information.html')
